package com.lingxu.module.jsc.controller;

import com.lingxu.base.common.api.vo.Result;
import com.lingxu.module.jsc.mapper.Statistics3Mapper;
import com.lingxu.module.zx.util.CheckVariable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/jsc3/tj")
@Api(tags = "驾驶舱3")
public class Jsc3Controller {

    @Autowired
    private Statistics3Mapper statistics3Mapper;


    @GetMapping("getMxly")
    @ApiOperation("模型领域")
    public Result getMxly(){

        List<Map<String, Object>> mxly = statistics3Mapper.getMxly();

        return Result.ok(mxly);
    }


    @GetMapping("getMxYyqkpm")
    @ApiOperation("模型应用情况排名")
    public Result getMxYyqkpm(){


        //统计模型应用次数前三的模型
        List<Map<String, Object>> mxpm = statistics3Mapper.getMxpm();
        if(CheckVariable.isEmpty(mxpm)){
            return Result.ok(new ArrayList<>());
        }
        Set<String> modelid = mxpm.stream().map(v -> v.get("MODELID").toString()).collect(Collectors.toSet());


        //统计指定模型的模型地区排名
        List<Map<String, Object>> mxDqpm = statistics3Mapper.getMxDqpm(modelid);
        if(CheckVariable.isEmpty(mxDqpm)){
            mxDqpm = new ArrayList<>();
        }

        //先分组后排序
        Map<String, List<Map<String, Object>>> collect = mxDqpm.stream().collect(Collectors.groupingBy(v -> v.get("MODELID").toString(), Collectors.collectingAndThen(Collectors.toList(), m -> m.stream().sorted(Comparator.comparing(c -> Integer.valueOf(c.get("RS").toString()))).collect(Collectors.toList()))));

        mxpm.forEach(v->{
             List<Map<String, Object>> mapList = collect.get(v.get("MODELID").toString());
             if(CheckVariable.isEmpty(mapList)){
                 mapList = new ArrayList<>();
             }
            v.put("area",mapList);
        });

        return Result.ok(mxpm);
    }


    @GetMapping("getSjtj")
    @ApiOperation("数据统计")
    public Result getSjtj( ){


        //查询数据总量
        Integer sjzl = statistics3Mapper.getSjzl();

        //查询模型算法
//        Integer mxgz = statistics3Mapper.getMxsf();

        //查询基础数据
//        Integer jcsj =statistics3Mapper.getjcsj();

        //查询发现异常数据
        Integer fxycsj = statistics3Mapper.getYcsj();

        //查看查实数据
//        Integer hcsj = statistics3Mapper.cssj();

        //查询处置人员
        Integer czry = statistics3Mapper.getCzry();


        Map<String,Object> res = new HashMap<>();
        res.put("sjzl",sjzl);
        res.put("mxgz",80);
        res.put("jcsj",60);
        res.put("fxycsj",fxycsj);
        res.put("hcsj",85);
        res.put("czry",czry);
        return Result.ok(res);
    }


    @GetMapping("getDqyyfb")
    @ApiOperation("地区应用分布")
    public Result getDqyyfb(){





        return Result.ok();
    }






}
